﻿using DLUProject.Domain;
using DLUProject.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ColorLife.Core.Helper;
public partial class KTX_NoticeDetail : BaseUserControl
{
    public ModuleInstanceExtension moduleInstance { get; set; }
    public IServices<ThongBao> _thongBaoService { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int IDThongBao
    {
        get
        {
            if (Request.QueryString["IDThongBao"] != null)
                return Request.QueryString["IDThongBao"].ToInt();
            return -1;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        
        if (!IsPostBack)
        {
            var thongBao = _thongBaoService.Get(IDThongBao);
            if(thongBao==null)
            {
                Response.Redirect("Default.aspx");
            }
            Page.Title = thongBao.TieuDe;

            lblTieuDe.Text = thongBao.TieuDe;
            lblNoiDung.Text = thongBao.NoiDung;
            lblNguoiThongBao.Text = thongBao.NguoiThongBao;
            lblLuotXem.Text = thongBao.LuotXem.ToString();
            lblNgayDang.Text = thongBao.NgayThongBao.ToShortDateString();

            thongBao.LuotXem++;
            _thongBaoService.Update(thongBao);

            List<ModuleInstance> modulesLeft = moduleInstance.GetByModulePosition(ModulePosition.Left);
            foreach (var item in modulesLeft)
            {
                BaseUserControl userControlX = (BaseUserControl)LoadControl(item.Module.Path);

                if (userControlX != null)
                {
                    userControlX.ModuleID = item.ModuleInstanceID.ToString();
                    userControlX.Title = item.Title;
                    userControlX.Params = item.Params;
                    userControlX.ShowHide = item.IsActive;
                    userControlX.State = item.State;
                    PlaceHolderRight.Controls.Add(userControlX);
                }
            }
            rptItems.DataSource = _thongBaoService.Table.OrderByDescending(c => c.GhimThongBao).Take(10);
            rptItems.DataBind();

            Repeater1.DataSource = _thongBaoService.Table.OrderByDescending(c => c.IDThongBao).Take(20);
            Repeater1.DataBind();

            rptHits.DataSource = _thongBaoService.Table.OrderByDescending(c => c.LuotXem).Take(20);
            rptHits.DataBind();
        }
    }
}